package com.ctshk.rpc.order.bus.service.impl;
import java.time.LocalDateTime;
import java.util.Objects;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OrderSource;
import com.ctshk.common.enums.ProductType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.BConst;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.bus.entity.BusManualOrder;
import com.ctshk.rpc.order.bus.entity.BusOrder;
import com.ctshk.rpc.order.bus.entity.BusOrderCost;
import com.ctshk.rpc.order.bus.entity.BusOrderTourist;
import com.ctshk.rpc.order.bus.mapper.BusManualOrderMapper;
import com.ctshk.rpc.order.bus.mapper.BusOrderCostMapper;
import com.ctshk.rpc.order.bus.mapper.BusOrderMapper;
import com.ctshk.rpc.order.bus.mapper.BusOrderTouristMapper;
import com.ctshk.rpc.order.bus.req.BusManualOrderReq;
import com.ctshk.rpc.order.bus.req.BusManualOrderTouristReq;
import com.ctshk.rpc.order.bus.req.BusOrderReq;
import com.ctshk.rpc.order.bus.service.IBusManualOrderService;
import com.ctshk.rpc.order.bus.service.IBusOrderService;
import com.ctshk.rpc.system.service.IMainDataCurrencyService;

/**
 * <p>
 * 巴士手工单（外挂单） 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-24
 */
@DubboService
public class BusManualOrderServiceImpl extends ServiceImpl<BusManualOrderMapper, BusManualOrder> implements IBusManualOrderService, BaseService {

    @Autowired
    private BusManualOrderMapper busManualOrderMapper;
    @Autowired
    private BusOrderMapper busOrderMapper;
    @Autowired
    private BusOrderTouristMapper busOrderTouristMapper;
    
    @Autowired
    BusOrderCostMapper busOrderCostMapper;

    @Autowired
    IBusOrderService busOrderService;
    
    @Autowired
    IMainDataCurrencyService mainDataCurrencyService;
    
    /**
     * 添加外挂但
     * @param busManualOrderReq
     * @param user
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Long> addBusManualOrder(BusManualOrderReq busManualOrderReq, TokenUser user) {
        if(Objects.isNull(busManualOrderReq)){
            return Result.failed(SystemError.BUS_ORDER_9001);
        }
        BusOrderReq busOrderReq = busManualOrderReq.getBusOrderReq();
        if(Objects.isNull(busOrderReq)){
            return Result.failed(SystemError.BUS_ORDER_9002);
        }

        LocalDateTime nowLocalDateTime = LocalDateTime.now();

        //添加订单信息
        BusOrder busOrder = newCreateEntity(BusOrder.class);
        BeanUtils.copyProperties(busOrderReq,busOrder);
        Long orderId = SnowflakeIdWorker.nextId();
        busOrder.setId(orderId);
        busOrder.setProductType(ProductType.PLUG.getCode());
        busOrder.setSource(OrderSource.CPM.getCode());
        busOrder.setEffectiveTime(DateUtil.toLocalDate(busOrderReq.getEffectiveTime()));
//        busOrder.setDepartureTime(DateUtil.toLocalTime(busOrderReq.getEffectiveTime()));
        busOrder.setContactName(Assist.join(busOrder.getSurname(), busOrder.getName()));
//        busOrder.setScheduledStatus(1); //待確認
        busOrder.setPaymentStatus(1); //待付款
//        busOrder.setCancelStatus(0); //未取消
        busOrder.setSalesmanId(user.getId());
        busOrder.setSalesmanName(user.getUsername());
        busOrder.setSalesMethods(1);//销售方式(1：单品销售 2：打包销售)
        busOrder.setIsAgent(BConst.ZERO);
        //获取巴士订单编号
        busOrder.setOrderNumber(busOrderService.buildOrderNumber(user.getId(), nowLocalDateTime));
        // 计算付款到期时间,+30分钟
        busOrder.setPayExpireDate(nowLocalDateTime.plusMinutes(30));
        
        
        busOrder.setOrderAmount(busManualOrderReq.getCostPrice());
        busOrder.setReceivableAmount(busManualOrderReq.getCostPrice());
        busOrderMapper.insert(busOrder);

        //添加外挂订单信息
        BusManualOrder busManualOrder = new BusManualOrder();
        BeanUtils.copyProperties(busManualOrderReq,busManualOrder);
        busManualOrder.setId(SnowflakeIdWorker.nextId());
        busManualOrder.setOrderId(orderId);
        busManualOrderMapper.insert(busManualOrder);
        
        //订单成本
        BusOrderCost busOrderCost = newCreateEntity(BusOrderCost.class);
        busOrderCost.setOrderId(busOrder.getId());
        busOrderCost.setCostPrice(busManualOrderReq.getCostPrice());
        busOrderCost.setCostCurrencyId(busManualOrderReq.getCostCurrencyId());
        busOrderCost.setCostExchangeRate(null);
        busOrderCost.setCostCurrency(busManualOrderReq.getCostCurrency());
        busOrderCost.setTouristType(1);
        busOrderCostMapper.insert(busOrderCost);
        
        //旅客列表
        if (Assist.isNotEmpty(busManualOrderReq.getTouristList())) {
        	for (BusManualOrderTouristReq touristReq : busManualOrderReq.getTouristList()) {
				BusOrderTourist touristEntity = newCreateEntity(BusOrderTourist.class, touristReq, user);
				touristEntity.setTouristType("1");
				touristEntity.setOrderId(busOrder.getId());
				touristEntity.setTouristName(Assist.join(touristEntity.getSurname(), touristEntity.getName()));
				touristEntity.setPrice(busOrderCost.getCostPrice());
				touristEntity.setCostId(busOrderCost.getId());
				busOrderTouristMapper.insert(touristEntity);
			}
        }

        return Result.success(orderId);
    }
}
